/*
 * @Version: V1.0
 * @Project: MGS组件 All rights Reserved, Designed By www.supermap.com
 * @Title:
 * @Package: mgs-webui-basic
 * @Description:
 * @Author: 李卫欣
 * @Date: 2021-08-08 16:04:40
 * @Copyright: 2021 www.supermap.com Inc. All rights reserved.
 * @注意: 本内容仅限于北京超图信息技术有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
// @ts-ignore
import proj4 from 'proj4'

//CGCS2000 6度分带坐标参考
export const prjSpatialRefs_CGCS2000 = [
    {
        srid: 4491,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 13",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",75],PARAMETER["scale_factor",1],PARAMETER["false_easting",13500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4491"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=75 +k=1 +x_0=13500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 72,
        lngEnd: 78,
        zone: 13,
    },
    {
        srid: 4492,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 14",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",81],PARAMETER["scale_factor",1],PARAMETER["false_easting",14500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4492"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=81 +k=1 +x_0=14500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 78,
        lngEnd: 84,
        zone: 14,
    },
    {
        srid: 4493,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 15",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",87],PARAMETER["scale_factor",1],PARAMETER["false_easting",15500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4493"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=87 +k=1 +x_0=15500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 84,
        lngEnd: 90,
        zone: 15,
    },
    {
        srid: 4494,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 16",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",93],PARAMETER["scale_factor",1],PARAMETER["false_easting",16500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4494"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=93 +k=1 +x_0=16500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 90,
        lngEnd: 96,
        zone: 16,
    },
    {
        srid: 4495,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 17",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",99],PARAMETER["scale_factor",1],PARAMETER["false_easting",17500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4495"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=99 +k=1 +x_0=17500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 96,
        lngEnd: 102,
        zone: 17,
    },
    {
        srid: 4496,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 18",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",105],PARAMETER["scale_factor",1],PARAMETER["false_easting",18500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4496"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=105 +k=1 +x_0=18500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 102,
        lngEnd: 108,
        zone: 18,
    },
    {
        srid: 4497,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 19",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",111],PARAMETER["scale_factor",1],PARAMETER["false_easting",19500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4497"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=111 +k=1 +x_0=19500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 108,
        lngEnd: 114,
        zone: 19,
    },
    {
        srid: 4498,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 20",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",117],PARAMETER["scale_factor",1],PARAMETER["false_easting",20500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4498"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=20500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 114,
        lngEnd: 120,
        zone: 20,
    },
    {
        srid: 4499,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 21",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",123],PARAMETER["scale_factor",1],PARAMETER["false_easting",21500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4499"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=123 +k=1 +x_0=21500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 120,
        lngEnd: 126,
        zone: 21,
    },
    {
        srid: 4500,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 22",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",129],PARAMETER["scale_factor",1],PARAMETER["false_easting",22500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4500"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=129 +k=1 +x_0=22500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 126,
        lngEnd: 132,
        zone: 22,
    },
    {
        srid: 4501,
        sr: 'PROJCS["CGCS2000 / Gauss-Kruger zone 23",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",135],PARAMETER["scale_factor",1],PARAMETER["false_easting",23500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4501"]]',
        proj4: '+proj=tmerc +lat_0=0 +lon_0=135 +k=1 +x_0=23500000 +y_0=0 +ellps=GRS80 +units=m +no_defs ',
        lngStart: 132,
        lngEnd: 138,
        zone: 23,
    },
]

export function getCGCS2000Prj(latlng: number[]) {
    const lng = latlng[0]
    const spatialReference = getCGCS2000SpatialReferenceFromLng(lng)
    return proj4(spatialReference.proj4).forward(latlng)
}

export function getWGS84_CGCS2000(pos: number[]) {
    const x = pos[0]
    const spatialReference = getCGCS2000SpatialReferenceFromX(x)
    const latlng = proj4(spatialReference.proj4).inverse(pos)
    if (latlng[0] < spatialReference.lngStart) {
        latlng[0] = spatialReference.lngStart
    }
    if (latlng[0] > spatialReference.lngEnd) {
        latlng[0] = spatialReference.lngEnd
    }
    return latlng
}

export function getCGCS2000SpatialReferenceFromX(x: number) {
    switch (x.toString().substr(0, 2)) {
        case '13':
            return prjSpatialRefs_CGCS2000[0]
        case '14':
            return prjSpatialRefs_CGCS2000[1]
        case '15':
            return prjSpatialRefs_CGCS2000[2]
        case '16':
            return prjSpatialRefs_CGCS2000[3]
        case '17':
            return prjSpatialRefs_CGCS2000[4]
        case '18':
            return prjSpatialRefs_CGCS2000[5]
        case '19':
            return prjSpatialRefs_CGCS2000[6]
        case '20':
            return prjSpatialRefs_CGCS2000[7]
        case '21':
            return prjSpatialRefs_CGCS2000[8]
        case '22':
            return prjSpatialRefs_CGCS2000[9]
        case '23':
            return prjSpatialRefs_CGCS2000[10]
    }
    throw new Error('不支持的代号x.')
}

export function getCGCS2000SpatialReferenceFromLng(lng: number) {
    if (lng >= 72 && lng <= 78) {
        //75度分带
        return prjSpatialRefs_CGCS2000[0]
    } else if (lng > 78 && lng <= 84) {
        //81度分带
        return prjSpatialRefs_CGCS2000[1]
    } else if (lng > 84 && lng <= 90) {
        //87度分带
        return prjSpatialRefs_CGCS2000[2]
    } else if (lng > 90 && lng <= 96) {
        //93度分带
        return prjSpatialRefs_CGCS2000[3]
    } else if (lng > 96 && lng <= 102) {
        //99度分带
        return prjSpatialRefs_CGCS2000[4]
    } else if (lng > 102 && lng <= 108) {
        //105度分带
        return prjSpatialRefs_CGCS2000[5]
    } else if (lng > 108 && lng <= 114) {
        //111度分带
        return prjSpatialRefs_CGCS2000[6]
    } else if (lng > 114 && lng <= 120) {
        //117度分带
        return prjSpatialRefs_CGCS2000[7]
    } else if (lng > 120 && lng <= 126) {
        //123度分带
        return prjSpatialRefs_CGCS2000[8]
    } else if (lng > 126 && lng <= 132) {
        //129度分带
        return prjSpatialRefs_CGCS2000[9]
    } else if (lng > 132 && lng <= 138) {
        //135度分带
        return prjSpatialRefs_CGCS2000[10]
    }
    throw new Error('不支持的代号l.')
}

export function getCGCS2000FDXFromLng(lng: number) {
    switch (lng) {
        case 72:
            return 13
        case 78:
            return 14
        case 84:
            return 15
        case 90:
            return 16
        case 96:
            return 17
        case 102:
            return 18
        case 108:
            return 19
        case 114:
            return 20
        case 120:
            return 21
        case 126:
            return 22
        case 132:
            return 23
        default:
            return 0
    }
}
